package com.ssy.service.impl;

import com.ssy.entity.Dept;
import com.ssy.mapper.DeptMapper;

import com.ssy.mapper.EmpMapper;
import com.ssy.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {


    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private EmpMapper empMapper;


    /**
     * 查询所有部门
     * @return
     */
    @Override
    public List<Dept> listAll() {
        return deptMapper.listAll();
    }

    /**
     * 删除部门
     * @param id
     */
    @Override
    public void deleteByID(Integer id) {
        // 检查该部门下是否有员工
        Long empCount = empMapper.countByDeptId(id);
        if (empCount != null && empCount > 0) {
            throw new RuntimeException("对不起，当前部门下有员工，不能直接删除！");
        }

        deptMapper.deleteByID(id);
    }

    /**
     * 添加部门
     * @param dept
     */
    @Override
    public void addDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.addDept(dept);
    }

    /**
     * 根据id查询部门
     */
    @Override
    public Dept getById(Integer id) {
        return deptMapper.getById(id);
    }

    /**
     * 修改部门
     * @param dept
     */
    @Override
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDept(dept);
    }


}
